package com.fer.controller;

import com.fer.model.Staff;
import com.fer.service.StaffService;
import com.fer.service.impl.StaffServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffService staffService;


    @GetMapping("/{shop_id}/{staff_name}")
    @ResponseBody
    public Map<String,Object> findStaff(@PathVariable("staff_name") String staff_name,@PathVariable("shop_id")Integer shop_id){
        Map<String,Object> result = new HashMap<>();
        List<Staff> staff=staffService.findStaff(staff_name,shop_id);
        if(staff==null){
            result.put("code",400);
            result.put("message","查找失败");
            return result;
        }
        result.put("code",200);
        result.put("message","查找成功");
        result.put("data",staff);
        return result;
    }
    @GetMapping("/{shop_id}")
    @ResponseBody
    public Map<String,Object> findAllStaff(@PathVariable("shop_id")Integer shop_id){
        Map<String,Object> result = new HashMap<>();
        List<Staff> staff=staffService.findStaff(null,shop_id);
        if(staff==null){
            result.put("code",400);
            result.put("message","查找失败");
            return result;
        }
        result.put("code",200);
        result.put("message","查找成功");
        result.put("data",staff);
        return result;

    }


    @PostMapping
    @ResponseBody
    public Map<String,Object> addStaff(Staff staff){
        Map<String,Object> result = new HashMap<>();
        System.out.println(staff);
        try{
            staffService.addStaff(staff);
        }catch (Exception e) {
            result.put("message","failed");
            result.put("code",400);
            System.out.println(e);
            System.out.println("failed!!!!!!!!!!!!!!!!!!!!!!");
            return result;
        }
        result.put("message","success");
        result.put("code",200);
        return result;
    }
    @DeleteMapping("/{staff_id}")
    @ResponseBody
    public Map<String,Object> deleteStaff(@PathVariable("staff_id") Integer staff_id){
        Map<String,Object> result = new HashMap<>();
        try{
            staffService.deleteStaff(staff_id);
        }catch (Exception e) {
            result.put("message","failed");
            result.put("code",400);
            System.out.println("failed2!!!!!!!!!!!!!!!!!!!!!!");
            return result;
        }
        result.put("message","success");
        result.put("code",200);
        return result;
    }
    @PutMapping
    @ResponseBody
    public Map<String,Object> modifyStaff(Staff staff){
        Map<String,Object> result = new HashMap<>();
        Boolean state=staffService.modifyStaff(staff);
        if(state=false){
            result.put("message","failed");
            result.put("code",400);
            System.out.println("failed3!!!!!!!!!!!!!!");
            return result;
        }
        result.put("message","success");
        result.put("code",200);
        return result;
    }
}
